/*
Copyright 2000-2011 Francois de Bertrand de Beuvron

This file is part of CoursBeuvron.

CoursBeuvron is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CoursBeuvron is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CoursBeuvron.  If not, see <http://www.gnu.org/licenses/>.
 */
package fr.insa.beuvron.tps.m2.dessin2DV3;

import fr.insa.commun.Lire;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;

public class Point implements Figure {

    /**
     * les point sont affichés comme des petits cercles ayant une certaine
     * taille défini en nombre de pixels
     */
    public static final double TAILLE_AFFICHAGE = 10;
    private double abscisse;
    private double ordonnee;

    public Point() {
    }

    public Point(double x, double y) {
        this.abscisse = x;
        this.ordonnee = y;
    }

    public static Point cree(double x, double y) {
        return new Point(x, y);
    }

    public static Point demande() {
        System.out.println("abscisse du point : ");
        double abs = Lire.d();
        System.out.println("ordonnée du point : ");
        double ord = Lire.d();
        return Point.cree(abs, ord);
    }

    public boolean egal(Point p2) {
        return (this.abscisse == p2.abscisse) && (this.ordonnee == p2.ordonnee);
    }

    @Override
    public String toString() {
        return "(" + Utils.formatDouble(this.abscisse,4) + "," + Utils.formatDouble(this.ordonnee,4) + ")";
    }

    public double getAbscisse() {
        return abscisse;
    }

    public void setAbscisse(double val) {
        this.abscisse = val;
    }

    public double getOrdonnee() {
        return ordonnee;
    }

    public void setOrdonnee(double val) {
        this.ordonnee = val;
    }

    @Override
    public double maxX() {
        return this.abscisse;
    }

    @Override
    public double minX() {
        return this.abscisse;
    }

    @Override
    public double maxY() {
        return this.ordonnee;
    }

    @Override
    public double minY() {
        return this.ordonnee;
    }

    @Override
    public double distance(Point p) {
        double dx = this.abscisse - p.abscisse;
        double dy = this.ordonnee - p.ordonnee;
        return Math.sqrt(dx * dx + dy * dy);
    }

    @Override
    public String jolieDescription(int niveau) {
        return Utils.indent("Point " + this.toString(), niveau * 2);
    }

    /**
     * affichage  : les points sont représentés par
     * des cercles de taille TAILLE_AFFICHAGE
     */
    public void dessine(Graphics2D g, AffineTransform curTrans) {
        Point2D p = new Point2D.Double(this.abscisse, this.ordonnee);
        curTrans.transform(p, p);
        Ellipse2D.Double e = new Ellipse2D.Double(
                p.getX() - TAILLE_AFFICHAGE/2,
                p.getY() - TAILLE_AFFICHAGE/2,
                TAILLE_AFFICHAGE,
                TAILLE_AFFICHAGE);
        g.fill(e);
    }
//    public void dessine(Graphics2D g, AffineTransform trans) {
//        double[] coords = new double[]{this.abscisse, this.ordonnee};
//        trans.transform(coords, 0, coords, 0, 1);
//        g.fillOval((int) (coords[0] - TAILLE_AFFICHAGE / 2),
//                (int) (coords[1] - TAILLE_AFFICHAGE / 2),
//                (int) TAILLE_AFFICHAGE, (int) TAILLE_AFFICHAGE);
//    }
}
